package com.meihua.shellapp.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.ClassroomKnowledgeAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.bean.ClassroomListBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class ClassroomKnowledgeActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.know_rv_content)
    IRecyclerView mRecyclerview;
    private ClassroomKnowledgeAdapter mAdapter;
    private LoadMoreFooterView loadMoreFooterView;
    private String type;
    private int mPage = 1;
    private List<ClassroomListBean.DataBean> mData;

    @Override
    public int getLayoutId() {
        return R.layout.activity_classroom_knowledge;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        type = getIntent().getStringExtra("type");
        tvTitle.setVisibility(View.VISIBLE);
        if ("1".equals(type)) {
            tvTitle.setText("名人讲堂");
        } else if ("2".equals(type)) {
            tvTitle.setText("销售技巧");
        } else if ("3".equals(type)) {
            tvTitle.setText("保险知识");
        } else if ("4".equals(type)) {
            tvTitle.setText("产品知识讲堂");
        } else if ("5".equals(type)) {
            tvTitle.setText("经验分享");
        }
        mIVTopLeft.setVisibility(View.VISIBLE);
        mIVTopLeft.setOnClickListener(this);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        loadMoreFooterView = (LoadMoreFooterView) mRecyclerview.getLoadMoreFooterView();
        mData = new ArrayList();
        mAdapter = new ClassroomKnowledgeAdapter(mData,this);
//        mAdapter.setOnItemClickListener(this);
        mRecyclerview.setIAdapter(mAdapter);
        mRecyclerview.setOnLoadMoreListener(this);
        mRecyclerview.setOnRefreshListener(this);
        if(type.equals("1")){
            type = "4";
        }else {
            if (type.equals("4")) {
                type = "1";
            }
        }
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        getServerData();
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        mPage++;
        getServerData();
        loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
    }

    /**
     * 刷新操作
     */
    @Override
    public void onRefresh() {
        super.onRefresh();
        mRecyclerview.setRefreshing(true);
        mPage = 1;
        getServerData();
        loadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
    }

    /**
     * 获取服务器数据
     */
    private void getServerData() {
        //1=>产品知识讲堂 2=>销售技巧 3=>保险知识 4=>名人讲堂5=>经验分享



        OkGo.<String>post(URL.getUrl().CLASSROOMGETLIST)
                .params("type", type)
                .params("page", mPage)
                .execute(new MyStringCallBack<ClassroomListBean>(mRecyclerview) {
                    @Override
                    public void onSuccessData(ClassroomListBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() != 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                            endRefreshing(mRecyclerview);
                        }
                    }
                });
    }

}
